#include "head.h"

/***************************************************************************************
 *                                注册、登入、退出								    *
 ***************************************************************************************/
/*
 * @Author:yhy
 * @LastAuthor:yhy
 */
bool sign_in(int serviceId,user_t* user){
	printf("请输入用户名：");
	fflush(stdout);
	char userName[1024];
	int readNum = read(STDIN_FILENO,userName,sizeof(userName));
	userName[readNum-1] = '\0';
	printf("输入密码:");
	fflush(stdout);
	char password[1024];
	readNum = read(STDIN_FILENO,password,sizeof(password));
	password[readNum-1] = '\0';
	printf("登入成功\n");
    strcpy(user->user_name, userName);
//printf("user_name: %s\n", user->user_name);

    user->path[0] = '/';

    char* p1 = user->path + 1;
    char* p2 = userName;
    while(*p1++ = *p2++)
        ;
    *(p1-1) = '/';
    *p1 = '\0';
	return true;
}
bool sign_up(int serviceId){
	char cmd[100] = "up"; 
	send(serviceId,cmd,sizeof(cmd),0);//向服务器发送需要的功能
	printf("请输入用户名：");
	fflush(stdout);
	char userName[1024];
	int readNum = read(STDIN_FILENO,userName,sizeof(userName));
	userName[readNum-1] = '\0';
	printf("readNum:%d,%s\n",readNum,userName);
	send(serviceId,userName,sizeof(userName),0);
	bool isUserName;
	recv(serviceId,&isUserName,sizeof(isUserName),0);
	while(isUserName != true){
		printf("用户名已存在，请重新输入用户名:");
		fflush(stdout);
		readNum = read(STDIN_FILENO,userName,sizeof(userName));
		userName[readNum-1] = '\0';
		printf("readNum:%d,%s\n",readNum,userName);
		send(serviceId,userName,sizeof(userName),0);

		recv(serviceId,&isUserName,sizeof(isUserName),0);
	}
	printf("输入密码:");
	fflush(stdout);
	char password[1024];
	readNum = read(STDIN_FILENO,password,sizeof(password));
	password[readNum-1] = '\0';

	char confpassword[1024];
	printf("确认密码:");
	fflush(stdout);
	readNum = read(STDIN_FILENO,confpassword,sizeof(confpassword));
	confpassword[readNum-1] = '\0';
	while(strcmp(password,confpassword) != 0){
		printf("确认密码不正确,请重新输入\n确认密码:");
		fflush(stdout);
		readNum = read(STDIN_FILENO,confpassword,sizeof(confpassword));
		confpassword[readNum-1] = '\0';

	}
	send(serviceId,password,sizeof(password),0);

	return true;
}

